﻿using System;
using DevExpress.XtraEditors.DXErrorProvider;
using PMS.Resources;
using PMS.BLL;
using PMS.Entity;
using PMS.Utils;

namespace PMS.ValidationRules
{
    class PasswordValidateRule: ConditionValidationRule
    {

        public string ConfirmPasswordNotMatch = "ConfirmPasswordNotMatch";
        private string InitString = "";
        private bool Require = false;
        public PasswordValidateRule(string initstring, bool require = false)
        {
            InitString = initstring;
            Require = require;
        }

        public override bool Validate(System.Windows.Forms.Control control, object value)
        {
            try
            {
                string ProductCode = DataConvertUltils.ToString(value);
                if (!string.IsNullOrEmpty(ProductCode))
                {
                    if (ProductCode != InitString)
                    {
                        ErrorText = Messages.GetString(ConfirmPasswordNotMatch);
                        return false;
                    }
                }
                else
                {
                    if (Require)
                    {
                        ErrorText = Messages.GetString(Constants.REQUIRE_FIELD);
                        return false;
                    }
                }

                return true;
            }
            catch
            {
                return true;
                //throw new Exception(ex.Message);
            }
        }
    }
}
